package com.organization.model.services.impl

import org.springframework.stereotype.Service
import com.organization.model.services.EmployeeService
import org.springframework.beans.factory.annotation.Autowired
import com.organization.services.CommonService
import com.organization.model.dao.EmployeeDao
import com.organization.model.Employee
import com.organization.model.Project
import javax.validation.Validator
import javax.validation.Validation
import javax.validation.ConstraintViolation
import com.organization.model.services.ProjectService

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 10.07.12
 * Time: 13:26
 * To change this template use File | Settings | File Templates.
 */
@Service("employeeService")
class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeDao employeeDao
    @Autowired
    CommonService commonService
    @Autowired
    ProjectService projectService

    def getEmployeeInfo(empId){
        Employee employee = get(commonService.nodeIdToDbId(empId))
        if (employee){
            def result = commonService.goodResponse
            def exclude = ['class', 'metaClass', 'constraints', 'belongsTo', 'hasMany', 'project', 'log', 'errors']
            employee.properties.grep({ !exclude.contains(it.key) }).each { result.putAt(it.key,it.value) }
            return result;
        }
        return commonService.badResponse
    }

    def updateEmployeeInfo(params){
        Employee employee = get(commonService.nodeIdToDbId(params.nodeId))
        if (employee){
            employee.lastName = params.lastName
            employee.firstName = params.firstName
            employee.workPosition = params.workPosition
            employee.hireDate = commonService.convertDateFromJson(params.hireDate)
            if (params.containsKey('extensionPhone'))
                employee.extensionPhone = params.extensionPhone
            if (params.containsKey('mobilePhone'))
                employee.mobilePhone = params.mobilePhone
            if (params.containsKey('homePhone'))
                employee.homePhone = params.homePhone
            if (params.containsKey('email'))
                employee.email = params.email
            if (params.containsKey('addressStreetCity'))
                employee.addressStreetCity = params.addressStreetCity
            if (params.containsKey('addressHouse'))
                employee.addressHouse = params.addressHouse
            if (params.containsKey('addressAaprtment'))
                employee.addressApartment = params.addressApartment
            if (params.containsKey('birthDate'))
                employee.birthDate = commonService.convertDateFromJson(params.birthDate)
            if (params.containsKey('gender'))
                employee.gender = params.gender

            //validation
            Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(employee);
            if (constraintViolations.size()){
                return commonService.parseConstraintsFromDomain(constraintViolations)
            }
            else{
                update(employee)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('newName',employee.toString())
                response.putAt('nodeId',params.nodeId)
                return response
            }
        }
        return commonService.badResponse
    }

    def addEmployee(params){
        Project proj = projectService.get(commonService.nodeIdToDbId(params.parentNodeId))
        if (proj){
            Employee employee = new Employee(
                    project: proj,
                    lastName: params.lastName,
                    firstName: params.firstName,
                    workPosition: params.workPosition,
                    extensionPhone: params.extensionPhone,
                    mobilePhone: params.mobilePhone,
                    homePhone: params.homePhone,
                    email: params.email,
                    addressStreetCity: params.addressStreetCity,
                    addressHouse: params.addressHouse,
                    addressApartment: params.addressApartment,
                    gender: params.gender,
                    hireDate: commonService.convertDateFromJson(params.hireDate),
                    birthDate: commonService.convertDateFromJson(params.birthDate)
            );
            //validation
            Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(employee);
            if (constraintViolations.size()){
                return commonService.parseConstraintsFromDomain(constraintViolations)
            }
            else{
                save(employee)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('nodeId',commonService.emplNodeStr+employee.id.toString())
                response.putAt('parentNodeId',params.parentNodeId)
                return response
            }
        }
        return commonService.badResponse
    }

    def deleteEmployee(params){
        Employee employee = get(commonService.nodeIdToDbId(params.nodeId))
        if (employee){
            delete(employee)
            return commonService.goodResponse
        }
        return commonService.badResponse;
    }

    @Override
    public void save(Employee empl){
        employeeDao.save(empl)
    }

    @Override
    public void update(Employee empl){
        employeeDao.update(empl)
    }

    @Override
    public void delete(Employee empl){
        employeeDao.delete(empl)
    }

    @Override
    public List<Employee> employeeList() {
        employeeDao.employeeList()
    }

    @Override
    public List<Employee> findAllByProject(Project proj){
        employeeDao.findAllByProject(proj)
    }

    @Override
    public Employee get(int id) {
        employeeDao.get(id)
    }

    //validator
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

}
